/*************************************************************************
 *
 *    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2010
 *
 *    File name   : sd_ll_spi.h
 *    Description : SD/MMC low level SPI driver include file
 *
 *    History :
 *    1. Date        : June, 2010
 *       Author      : Stoyan Choynev
 *       Description : Initial revition
 *
 *    $Revision: 40213 $
 **************************************************************************/

#ifndef __SD_LL_SPI1_H
#define __SD_LL_SPI1_H

#include "includes.h"
//#include "mcu.h"

/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define')                           */
/*****************************************************************************/
#ifdef SD_LL_SPI1_GLOBAL
#define SD_LL_SPI1_EXTERN
#else
#define SD_LL_SPI1_EXTERN  extern
#endif
/* clock define*/
#ifndef CLOCK_FREQ
    #ifdef __PLLCLK
        #define CLOCK_FREQ __PLLCLK
    #else
        #define CLOCK_FREQ 	40000000UL
    #endif
#endif

#define SD_DLY_1MSEC       1000

/* SD Card */
#define SD_CS         (1<<4)
#define SD_CS_PFR     FM3_GPIO->PFR3
#define SD_CS_DDR     FM3_GPIO->DDR3
#define SD_CS_PDOR    FM3_GPIO->PDOR3

#define SD_CP         (1<<5)
#define SD_CP_PFR     FM3_GPIO->PFR3
#define SD_CP_DDR     FM3_GPIO->DDR3
#define SD_CP_PDIR    FM3_GPIO->PDIR3

#define SD_WP
#define SD_WP_PFR
#define SD_WP_DDR
#define SD_WP_PDIR

#define SD_CSIO_CH		FM3_MFS5_CSIO
#define SD_CSIO_MASK	(1<<6)|(1<<7)|(1<<8)
#define SD_CSIO_PFR		FM3_GPIO->PFR3
#define SD_CSIO_DDR		FM3_GPIO->DDR3
#define SD_CSIO_EPFR	FM3_GPIO->EPFR08
#define SD_CSIO_EPFR_MASK (3<<10)|(3<<12)|(3<<14)
#define SD_CSIO_EPFR_VAL (3<<10)|(3<<12)|(3<<14)


int32_t GetBaseClock(void);
/*************************************************************************
 * Function Name: SdPowerOn
 * Parameters: none
 * Return: none
 *
 * Description: Set power off state
 *
 *************************************************************************/
void SdPowerOn (void);

/*************************************************************************
 * Function Name: SdPowerOff
 * Parameters: none
 * Return: none
 *
 * Description: Set power off state
 *
 *************************************************************************/
void SdPowerOff (void);

/*************************************************************************
 * Function Name: SdChipSelect
 * Parameters: boolean_t Select
 * Return: none
 *
 * Description: SD/MMC Chip select control
 * Select = true  - Chip is enable
 * Select = false - Chip is disable
 *
 *************************************************************************/
void SdChipSelect (boolean_t Select);

/*************************************************************************
 * Function Name: SdPresent
 * Parameters: none
 * Return: boolean_t - true cart present
 *                 - false cart no present
 *
 * Description: SD/MMC precent check
 *
 *************************************************************************/
boolean_t SdPresent(void);

/*************************************************************************
 * Function Name: SdWriteProtect
 * Parameters: none
 * Return: boolean_t - true cart is protected
 *                 - false cart no protected
 *
 * Description: SD/MMC Write protect check
 *
 *************************************************************************/
boolean_t SdWriteProtect (void);

/*************************************************************************
 * Function Name: SdSetClockFreq
 * Parameters: uint32_t Frequency
 * Return: uint32_t
 *
 * Description: Set SPI ckl frequency
 *
 *************************************************************************/
uint32_t SdSetClockFreq (uint32_t Frequency);

/*************************************************************************
 * Function Name: SdInit
 * Parameters: none
 * Return: none
 *
 * Description: Init SPI, Cart Present, Write Protect and Chip select pins
 *
 *************************************************************************/
void SdInit (void);

/*************************************************************************
 * Function Name: SdTranserByte
 * Parameters: uint8_t ch
 * Return: uint8_t
 *
 * Description: Read byte from SPI
 *
 *************************************************************************/
uint8_t SdTranserByte (uint8_t ch);

/*************************************************************************
 * Function Name: SdSendBlock
 * Parameters: uint8_t * pData, uint32_t Size
 *
 * Return: void
 *
 * Description: Read byte from SPI
 *
 *************************************************************************/
void SdSendBlock (uint8_t * pData, uint32_t Size);

/*************************************************************************
 * Function Name: SdReceiveBlock
 * Parameters: uint8_t * pData, uint32_t Size
 *
 * Return: void
 *
 * Description: Read byte from SPI
 *
 *************************************************************************/
void SdReceiveBlock (uint8_t * pData, uint32_t Size);

/*************************************************************************
 * Function Name: SdDly_1ms
 * Parameters: uint32_t Delay
 * Return: none
 *
 * Description: Delay [msec]
 *
 *************************************************************************/
void SdDly_1ms (uint32_t Delay);

#endif // __SD_LL_SPI1_H
